Imports System.Collections

Partial Class pages_products_usercontrols_UCProduct : Inherits UserControl

#Region "Members"
    Private m_product As CProduct
    Private m_sortedList As IList
#End Region

#Region "Interface"
    Public Sub Display(ByVal [product] As CProduct, sortedList As IList, showDetails As Boolean, showPictures As Boolean, showPricing As Boolean, showContent As Boolean, showTotals As Boolean, showOrder As Boolean)
        If Parent.Controls.Count Mod 2 = 0 Then litRowAttr.Text = "class=alt_row"

        m_product = [product]
        m_sortedList = sortedList

        plhDetails.Visible = showDetails
        plhContents.Visible = showContent
        plhPayments.Visible = showTotals
        plhPricing.Visible = showPricing
        plhOrder.Visible = showOrder

        ddButton.DataSource = CButton.Cache
        ddButton.DataBind()
        CDropdown.BlankItem(ddButton)

        With m_product
            litNumber.Text = CStr(sortedList.IndexOf(m_product) + 1)
            litProductTypeId.Text = .ProductType.TypeName
            lnkProductName.Text = CStr(IIf(.ProductName.Length = 0, "...", .ProductName))
            lnkProductName.NavigateUrl = CSitemap.ProductEdit(.ProductId)
            If .HasImage Then
                lnkProductImage.ImageUrl = CSitemap.ProductTiny(m_product)
                lnkProductImage.NavigateUrl = CSitemap.ProductImage(.ProductId)
                lnkProductImage.ToolTip = lnkProductName.ToolTip
                lnkProductImage.Visible = showPictures
            End If
            If Not .IsEnglish Then
                lnkNameChineseSimplified.Visible = True
                lnkNameChineseSimplified.Text = .ProductNameChineseSimplified
                lnkNameChineseSimplified.NavigateUrl = lnkProductName.NavigateUrl
            End If

            lnkBelongsToName.Text = CUtilities.Truncate(.BelongsToName)
            lnkBelongsToName.ToolTip = .BelongsToName
            lnkBelongsToName.NavigateUrl = CSitemap.ProductEdit(.ProductBelongsToProductId)

            litProductDescription.ToolTip = .ProductDescription
            litProductDescription.Text = CUtilities.Truncate(Server.HtmlEncode(.ProductDescription))
            litProductKeywords.ToolTip = .ProductKeywords
            litProductKeywords.Text = CUtilities.Truncate(Server.HtmlEncode(.ProductKeywords))
            lnkButtonId.Text = .ProductPaypalCode
            lnkButtonId.ToolTip = .ButtonName
            lnkButtonId.NavigateUrl = CSitemap.External_Paypal(.ProductPaypalCode)
            CDropdown.SetValue(ddButton, .ProductPaypalButtonId)

            If .ProductIsActive AndAlso Not .ProductType.TypeIsFree Then
                If IsNothing(.Button) Then
                    lnkButtonId.ForeColor = Drawing.Color.Red
                    lnkButtonId.Text = "*Choose a button or mark as free"
                End If
            End If
            chkProductIsActive.ID = "chka_" & .ProductId
            chkProductIsActive.Checked = .ProductIsActive
            litProductCreated.ToolTip = CUtilities.LongDateTime(.ProductCreated)
            litProductCreated.Text = CUtilities.Timespan(.ProductCreated)
            If .ShowDate Then
                litProductCreated.ToolTip = litProductCreated.Text
                litProductCreated.Text = .DateString
            End If
            lnkLanguage.Text = .LanguageName
            lnkLanguage.ToolTip = .LanguageNative
            lnkLanguage.NavigateUrl = CSitemap.LanguageEdit(.ProductLanguageId)

            lnkYouTube.NavigateUrl = CSitemap.ProductVimeo(.ProductId)
            lnkVimeo.NavigateUrl = CSitemap.ProductVimeo(.ProductId)
            If .ProductVimeoId > 0 Then lnkVimeo.Text = .ProductVimeoId Else lnkVimeo.Text = "..."
            If Not String.IsNullOrEmpty(.ProductYouTubeId) Then lnkYouTube.Text = .ProductYouTubeId Else lnkYouTube.Text = "..."

            lnkSpeaker.Text = .Speaker.SpeakerName
            lnkSpeaker.NavigateUrl = CSitemap.SpeakerEdit(.ProductSpeakerId)

            lnkLocation.ToolTip = .Location.LocationName
            lnkLocation.Text = CUtilities.Truncate(.Location.LocationName, 20)
            lnkLocation.NavigateUrl = CSitemap.LocationEdit(.ProductLocationId)

            lnkTranscription.NavigateUrl = CSitemap.ProductTranscription(.ProductId)
            If .HasTranscript Then
                lnkTranscription.Text = .ScribeName
                lnkTranscription.ToolTip = CUtilities.Truncate(.ProductTranscription, 100)
            ElseIf .CanHaveTranscript Then
                lnkTranscription.Text = "Add..."
            End If

            lnkAmazon.NavigateUrl = CSitemap.ProductEdit(.ProductId)
            If Not String.IsNullOrEmpty(.ProductAmazonId) Then
                lnkAmazon.Text = .ProductAmazonId
            ElseIf .HasParent AndAlso Not String.IsNullOrEmpty(.BelongsTo.ProductAmazonId) Then
                lnkAmazon.NavigateUrl = CSitemap.ProductEdit(.ProductBelongsToProductId)
                lnkAmazon.Text = .BelongsTo.ProductAmazonId
            Else
                lnkAmazon.Text = "Add..."
            End If

            lnkSmashwords.NavigateUrl = CSitemap.ProductEdit(.ProductId)
            If .ProductSmashwordsId > 0 Then
                lnkSmashwords.Text = .ProductSmashwordsId
            ElseIf .HasParent AndAlso .BelongsTo.ProductSmashwordsId > 0 Then
                lnkSmashwords.NavigateUrl = CSitemap.ProductEdit(.ProductBelongsToProductId)
                lnkSmashwords.Text = .BelongsTo.ProductSmashwordsId
            Else
                lnkSmashwords.Text = "Add..."
            End If

            lnkCreatespace.NavigateUrl = lnkSmashwords.NavigateUrl
            If .ProductCSBookId > 0 Then
                lnkCreatespace.Text = .ProductCSBookId
            ElseIf .HasParent AndAlso .BelongsTo.ProductCSBookId > 0 Then
                lnkCreatespace.Text = .BelongsTo.ProductCSBookId
            Else
                lnkCreatespace.Text = "Add..."
            End If

            If showTotals Then

                Dim count As Integer = .OrdersCountCached

                If count > 0 Then
                    Dim value As Decimal = .SalesValueCached
                    Dim paid As Decimal = .SalesPaidCached
                    lnkOrders.Text = CUtilities.CountSummary(count, "order", "none")
                    lnkOrders.NavigateUrl = CSitemap.OrdersForProduct(.ProductId)
                    litSales.Text = CTextbox.SetMoney(value)
                    If paid > 0 Then litPaid.Text = CTextbox.SetMoney(paid)
                    If paid > 0 AndAlso value > 0 Then litConversion.Text = CTextbox.SetPercent(100.0 * paid / value, "n1")
                End If

                count = .PaymentsCountCached
                If count > 0 Then
                    Dim value As Decimal = .PaymentsSumOkCached
                    lnkPaymentsCount.Text = CUtilities.CountSummary(count, "payment", "none")
                    lnkPaymentsCount.NavigateUrl = CSitemap.PaymentsForProduct(.ProductId)

                    litPaymentsTotal.Text = CTextbox.SetMoney(value)
                End If

                If litPaid.Text <> litPaymentsTotal.Text Then
                    litPaid.ForeColor = Drawing.Color.Red
                    litPaymentsTotal.ForeColor = Drawing.Color.Red
                    litPaid.ToolTip = "Values should match"
                    litPaymentsTotal.ToolTip = "Values should match"
                End If

            End If

            lnkFilesOrSets.Text = .Contents
            If Not .IsSet Then
                lnkFilesOrSets.ToolTip = .Files.NamesAndSizes
                lnkFilesOrSets.NavigateUrl = CSitemap.ProductFiles(.ProductId)
            Else
                lnkFilesOrSets.ToolTip = .Children.NamesAndPrices & vbCrLf & .Files.NamesAndSizesShort
                lnkFilesOrSets.NavigateUrl = CSitemap.ProductContents(.ProductId)
            End If

            If showOrder Then
                CTextbox.SetNumber(txtOrder, .ProductBelongsToSortOrder)
                CTextbox.RightAlign(txtOrder)
            End If


            ''Show Files (Resource Type Products)
            'For Each i As CFile In .Files
            '    Dim li As New HtmlGenericControl("li")
            '    plh.Controls.Add(li)

            '    Dim lnk As New HyperLink
            '    li.Controls.Add(lnk)

            '    lnk.Text = i.FileName
            '    If i.FileSize > 0 Then lnk.Text = String.Concat(i.FileName, " (", CUtilities.FileSize(i.FileSize), ")")
            '    lnk.NavigateUrl = CConfig.DropboxUrl(i.FileName)
            'Next

            ''Show Files (Set Type Products)
            'For Each i As CSet In .ContainsSets
            '    Dim li As New HtmlGenericControl("li")
            '    plh.Controls.Add(li)

            '    Dim lnk As New HyperLink
            '    li.Controls.Add(lnk)

            '    lnk.NavigateUrl = CSitemap.ProductEdit(i.SetChildProductId)
            '    lnk.Text = i.ContainsProduct.ProductName
            '    lnk.NavigateUrl = i.ContainsProduct.ProductDescription
            'Next
        End With
    End Sub
#End Region

#Region "Event Handlers"
    Private Sub chkProductIsActive_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkProductIsActive.CheckedChanged
        With m_product
            .ProductIsActive = chkProductIsActive.Checked
            .Save()
        End With
        Refresh()
    End Sub
    Private Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnDelete.Click
        m_product.Delete()
        If IO.File.Exists(Server.MapPath(CSitemap.ProductImage(m_product))) Then IO.File.Delete((Server.MapPath(CSitemap.ProductImage(m_product))))
        If IO.File.Exists(Server.MapPath(CSitemap.ProductThumb(m_product))) Then IO.File.Delete((Server.MapPath(CSitemap.ProductThumb(m_product))))
        If IO.File.Exists(Server.MapPath(CSitemap.ProductTiny(m_product))) Then IO.File.Delete((Server.MapPath(CSitemap.ProductTiny(m_product))))
        Refresh()
    End Sub
    Protected Sub ddButton_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles ddButton.SelectedIndexChanged
        m_product.ProductPaypalButtonId = CDropdown.GetInt(ddButton)
        m_product.Save()
        Refresh()
    End Sub

    Protected Sub btnMoveDn_Click(sender As Object, e As System.Web.UI.ImageClickEventArgs) Handles btnMoveDn.Click
        With New CProductList(m_sortedList)
            .MoveDown(m_product)
        End With
        Refresh()
    End Sub

    Protected Sub btnMoveUp_Click(sender As Object, e As System.Web.UI.ImageClickEventArgs) Handles btnMoveUp.Click
        With New CProductList(m_sortedList)
            .MoveUp(m_product)
        End With
        Refresh()
    End Sub

    Protected Sub txtOrder_TextChanged(sender As Object, e As System.EventArgs) Handles txtOrder.TextChanged
        With New CProductList(m_sortedList)
            .MoveTo(m_product, CTextbox.GetInteger(txtOrder))
        End With
        Refresh()
    End Sub
#End Region

#Region "Private"
    Private Sub Refresh()
        'CCache.ClearCache()

        'If False Then 'Request.RawUrl.ToLower.Contains("myparent.aspx") Then
        '    'Special case: Parent entity owns the list
        '    Response.Redirect(CSitemap.MyParentEdit(m_product.ProductParentId, MyParent.ETab.products, m_pageIndex)
        'Else
        '    'Normal case: Search page owns the list
            Response.Redirect(Request.RawUrl) 'includes paging info
        'End If 
    End Sub
#End Region

End Class
